package cn.mybatishelper.handler;

import cn.mybatishelper.entity.CreateFile;
import cn.mybatishelper.entity.database.DatabaseEntity;
import cn.mybatishelper.exception.MybatisHelperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 默认处理器
 * User: GameKing
 */
public class DefaultHandler extends AbstractHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultHandler.class);

    public DefaultHandler() {
    }

    public DefaultHandler(List<DatabaseEntity> entities, List<CreateFile> fileEntities) {
        super(entities, fileEntities);
    }

    @Override
    public void execute() throws MybatisHelperException {
        if (!validate()) {
            throw new MybatisHelperException("执行Handler时，验证数据完整性发生异常...");
        }
        for (DatabaseEntity databaseEntity : entities) {
            createFile(databaseEntity);
        }
    }

    private void createFile(DatabaseEntity databaseEntity) throws MybatisHelperException {
        if (databaseEntity == null) {
            return;
        }
        for (CreateFile createFile : fileEntities) {
            createFile.createFile(databaseEntity);
        }
    }
}
